package com.school.serviceImpl;

import com.school.dao.DepartmentMapper;
import com.school.dao.RoleMapper;
import com.school.model.Department;
import com.school.model.Role;
import com.school.model.Score;
import com.school.model.User;
import com.school.service.DepartmentService;
import com.school.service.RoleService;
import com.school.service.ScoreService;
import com.school.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired(required = false)
    private DepartmentMapper departmentMapper;

    @Autowired
    private UserService userService;
    @Autowired
    private ScoreService scoreService;


    @Override
    public synchronized int insertSelective(Department record) {
        if(departmentMapper.selectByName(record.getName())!=null){
            return  -1;
        }
        record.setCreateTime(new Date());
        record.setIsDelete(1);
        return departmentMapper.insertSelective(record);
    }

    @Override
    public synchronized int updateByPrimaryKeySelective(Department record) {
        if(departmentMapper.selectByName(record.getName())!=null){
            return  -1;
        }
        userService.updateDepartmentNameByDepId(record.getId(),record.getName());
        record.setModifyTime(new Date());
        return departmentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public synchronized int deleteByPrimaryKey(Integer id) {
        User user= new User();
        user.setDepartmentId(id);
        user.setModifyTime(new Date());
        userService.updateByDep(user);
        Score score=new Score();
        score.settDep(id);
        score.setModifyTime(new Date());
        scoreService.deleteByDepId(score);
        Department department=new Department();
        department.setId(id);
        department.setIsDelete(2);
        department.setModifyTime(new Date());
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public Department selectByPrimaryKey(Integer id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> getArray() {
        return departmentMapper.getArray();
    }

}
